; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=simplifycfg,adce -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s

declare void @f1()

declare void @f2()

declare void @f3()

declare void @f4()

define i32 @test1(i32 %X, i1 %D) {
; CHECK-LABEL: @test1(
; CHECK-NEXT:  E:
; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT:    br i1 [[C]], label [[B:%.*]], label [[F:%.*]]
; CHECK:       common.ret:
; CHECK-NEXT:    [[COMMON_RET_OP:%.*]] = phi i32 [ 345, [[B]] ], [ 123, [[F]] ]
; CHECK-NEXT:    ret i32 [[COMMON_RET_OP]]
; CHECK:       B:
; CHECK-NEXT:    call void @f2()
; CHECK-NEXT:    br label [[COMMON_RET:%.*]]
; CHECK:       F:
; CHECK-NEXT:    call void @f3()
; CHECK-NEXT:    br label [[COMMON_RET]]
;
E:
  %C = icmp eq i32 %X, 0		; <i1> [#uses=2]
  br i1 %C, label %T, label %F
T:		; preds = %A, %E
  br i1 %C, label %B, label %A
A:		; preds = %T
  call void @f1( )
  br i1 %D, label %T, label %F
B:		; preds = %T
  call void @f2( )
  ret i32 345
F:		; preds = %A, %E
  call void @f3( )
  ret i32 123
}

define i32 @test2(i32 %X, i1 %D) {
; CHECK-LABEL: @test2(
; CHECK-NEXT:  E:
; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT:    br i1 [[C]], label [[B:%.*]], label [[F:%.*]]
; CHECK:       common.ret:
; CHECK-NEXT:    [[COMMON_RET_OP:%.*]] = phi i32 [ 345, [[B]] ], [ 123, [[F]] ]
; CHECK-NEXT:    ret i32 [[COMMON_RET_OP]]
; CHECK:       B:
; CHECK-NEXT:    call void @f2()
; CHECK-NEXT:    br label [[COMMON_RET:%.*]]
; CHECK:       F:
; CHECK-NEXT:    call void @f3()
; CHECK-NEXT:    br label [[COMMON_RET]]
;
E:
  %C = icmp eq i32 %X, 0		; <i1> [#uses=2]
  br i1 %C, label %T, label %F
T:		; preds = %A, %E
  %P = phi i1 [ true, %E ], [ %C, %A ]		; <i1> [#uses=1]
  br i1 %P, label %B, label %A
A:		; preds = %T
  call void @f1( )
  br i1 %D, label %T, label %F
B:		; preds = %T
  call void @f2( )
  ret i32 345
F:		; preds = %A, %E
  call void @f3( )
  ret i32 123
}

define i32 @test3(i32 %X, i1 %D, ptr %AP, ptr %BP) {
; CHECK-LABEL: @test3(
; CHECK-NEXT:  E:
; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT:    br i1 [[C]], label [[B_CRITEDGE:%.*]], label [[F:%.*]]
; CHECK:       common.ret:
; CHECK-NEXT:    [[COMMON_RET_OP:%.*]] = phi i32 [ 345, [[B_CRITEDGE]] ], [ 123, [[F]] ]
; CHECK-NEXT:    ret i32 [[COMMON_RET_OP]]
; CHECK:       B.critedge:
; CHECK-NEXT:    call void @f3()
; CHECK-NEXT:    [[XX_C:%.*]] = load i32, ptr [[AP:%.*]], align 4
; CHECK-NEXT:    store i32 [[XX_C]], ptr [[BP:%.*]], align 4
; CHECK-NEXT:    call void @f2()
; CHECK-NEXT:    br label [[COMMON_RET:%.*]]
; CHECK:       F:
; CHECK-NEXT:    call void @f3()
; CHECK-NEXT:    br label [[COMMON_RET]]
;
E:
  %C = icmp eq i32 %X, 0		; <i1> [#uses=2]
  br i1 %C, label %T, label %F
T:		; preds = %A, %E
  call void @f3( )
  %XX = load i32, ptr %AP		; <i32> [#uses=1]
  store i32 %XX, ptr %BP
  br i1 %C, label %B, label %A
A:		; preds = %T
  call void @f1( )
  br i1 %D, label %T, label %F
B:		; preds = %T
  call void @f2( )
  ret i32 345
F:		; preds = %A, %E
  call void @f3( )
  ret i32 123
}
